package com.example.studywords;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.GridView;
import android.widget.SearchView;
import android.widget.Toast;

import com.example.studywords.adapter.GridViewAdapter;
import com.example.studywords.bean.WordBean;
import com.example.studywords.dlgs.AddWordDlg;
import com.example.studywords.sql.MyDatabase;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private Toolbar mainToolBar;
    private GridView mainGridView;
    private GridViewAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mainToolBar = findViewById(R.id.mainToolBar);
        mainGridView = findViewById(R.id.mainGridView);


        setToolbarListener();

        setGridData();

    }

    private void setToolbarListener(){
        mainToolBar.inflateMenu(R.menu.toolbar_menu);
        mainToolBar.setOnMenuItemClickListener(item -> {
            switch (item.getItemId()) {
                case R.id.app_bar_add:
                    AddWordDlg dlg = new AddWordDlg();
                    dlg.show(getSupportFragmentManager(), "add_word");
                    return true;
                case R.id.app_bar_refresh:
                    adapter.reReadDataAndRefresh();
                    return true;
                case R.id.app_bar_search:
                    SearchView searchView = (SearchView) mainToolBar.getMenu().findItem(R.id.app_bar_search).getActionView();
                    searchView.setQueryHint("格式为:[符号 数]");
                    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                        @Override
                        public boolean onQueryTextSubmit(String s) {
                            adapter.searchDataAndRefresh(s);
                            return false;
                        }

                        @Override
                        public boolean onQueryTextChange(String s) {
                            return false;
                        }
                    });
                    return true;
                case R.id.app_bar_help:
                    String searchHelp = "查询符号大小写不限\n" +
                            "t(=time) 查询正确次数等于此数值\n" +
                            "d(=day) 查询超过d没有学习的单词\n" +
                            "dt 超过d天没有学习,且正确次数小于t的单词";
                    Toast.makeText(this, searchHelp, Toast.LENGTH_LONG).show();
                    return true;
            }
            return true;
        });
    }

    private void setGridData(){
        new Thread(()->{
            List<WordBean> words = MyDatabase.getDatabase(this).wordDao().selectAll();
            adapter = new GridViewAdapter(words,this);
            runOnUiThread(()->mainGridView.setAdapter(adapter));
        }).start();

    }
}